package cn.iocoder.yudao.module.jky.controller.admin.goodscat;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.jky.controller.admin.goodscat.vo.*;
import cn.iocoder.yudao.module.jky.convert.goodscat.GoodsCatConvert;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodscat.GoodsCatDO;
import cn.iocoder.yudao.module.jky.service.goodscat.GoodsCatService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 商品分类")
@RestController
@RequestMapping("/supplier/goods-cat")
@Validated
public class GoodsCatController {

    @Resource
    private GoodsCatService goodsCatService;

    @PostMapping("/create")
    @Operation(summary = "创建商品分类")
    @PreAuthorize("@ss.hasPermission('jky:goods-cat:create')")
    public CommonResult<Short> createGoodsCat(@Valid @RequestBody GoodsCatCreateReqVO createReqVO) {
        return success(goodsCatService.createGoodsCat(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品分类")
    @PreAuthorize("@ss.hasPermission('jky:goods-cat:update')")
    public CommonResult<Boolean> updateGoodsCat(@Valid @RequestBody GoodsCatUpdateReqVO updateReqVO) {
        goodsCatService.updateGoodsCat(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateState")
    @Operation(summary = "更新商品状态")
    @PreAuthorize("@ss.hasPermission('jky:goods-cat:update')")
    public CommonResult<Boolean> updateGoodsCat(@RequestBody GoodsCatUpdateStateReqVO updateStateReqVO) {
        goodsCatService.updateGoodsCatState(updateStateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jky:goods-cat:delete')")
    public CommonResult<Boolean> deleteGoodsCat(@RequestParam("id") Short id) {
        goodsCatService.deleteGoodsCat(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jky:goods-cat:query')")
    public CommonResult<GoodsCatRespVO> getGoodsCat(@RequestParam("id") Short id) {
        GoodsCatDO goodsCat = goodsCatService.getGoodsCat(id);
        return success(GoodsCatConvert.INSTANCE.convert(goodsCat));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商品分类列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('jky:goods-cat:query')")
    public CommonResult<List<GoodsCatRespVO>> getGoodsCatList(GoodsCatExportReqVO exportReqVO) {
        List<GoodsCatDO> list = goodsCatService.getGoodsCatList(exportReqVO);
        return success(GoodsCatConvert.INSTANCE.convertList(list));
    }


    @GetMapping("/page")
    @Operation(summary = "获得商品分类分页")
    @PreAuthorize("@ss.hasPermission('jky:goods-cat:query')")
    public CommonResult<PageResult<GoodsCatRespVO>> getGoodsCatPage(@Valid GoodsCatPageReqVO pageVO) {
        PageResult<GoodsCatDO> pageResult = goodsCatService.getGoodsCatPage(pageVO);
        return success(GoodsCatConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品分类 Excel")
    @PreAuthorize("@ss.hasPermission('jky:goods-cat:export')")
    @OperateLog(type = EXPORT)
    public void exportGoodsCatExcel(@Valid GoodsCatExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GoodsCatDO> list = goodsCatService.getGoodsCatList(exportReqVO);
        // 导出 Excel
        List<GoodsCatExcelVO> datas = GoodsCatConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品分类.xls", "数据", GoodsCatExcelVO.class, datas);
    }

}
